package com.huas.controller;

import com.huas.common.Result;
import com.huas.dto.UserCodeDTO;
import com.huas.entity.User;
import com.huas.service.LoginService;
import com.huas.vo.UserCodeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


@RestController
@Slf4j
@RequestMapping("/system")
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 登录接口
     */
    @PostMapping("/login")
    public Result<String> Login(@RequestBody UserCodeDTO userCodeDTO){
        log.info("该用户正在登录:{}",userCodeDTO);
        return Result.success(loginService.login(userCodeDTO));

    }
    /**
     * 获取验证码
     * @return
     */
    @GetMapping("/captcha")
    public Result<UserCodeVO> getCaptcha(){
        return Result.success(loginService.getCaptcha());
    }

    /**
     * 注册接口
     */
    @PostMapping("/register")
    public Result register(@RequestBody User user){
        log.info("用户注册信息为：{}",user);
        loginService.register(user);
        return Result.success("注册成功");
    }
    /**
     * 退出登录接口
     */
    @PostMapping("/logout")
    @PreAuthorize("hasAuthority('system:logout')")
    public Result logout(){
        log.info("退出登录");
        loginService.logout();
        return Result.success();
    }
}
